﻿@page "/"
@using System.Text.Json

@inject IWebHostEnvironment even
@using HelloApp.Server.Components.ViewModel

@inject IDataService<Material> mats
@inject IDataService<MattressConfig> cons

@attribute [TabItemOption(Text = "定制", Closable = false)]

<div @onmouseenter="@(()=> Show_JingXiao =  true)" @onmouseleave="@(()=>Show_JingXiao = false)">
    <h6>长/宽超过 2.2米，无法压缩，只能手工卷包，体积会略大。</h6>
    <h6>薄垫厚度不能超过15cm
        @if (Show_JingXiao)
        {
            <span class="fs-6">@Price_JingXiao</span>
        }
    </h6>
</div>

<h3 class="text-success mb-3 mt-5">价格 @Price  元</h3>


<h4>尺寸</h4>
<div class="row mb-3">
    <div class="col-md-4">
        <BootstrapInputGroup>
            <BootstrapInputGroupLabel DisplayText="长" />
            <BootstrapInputNumber @bind-Value="@Length" Min="20" Max="400" />
            <BootstrapInputGroupLabel DisplayText="cm(厘米)" />
        </BootstrapInputGroup>
    </div>
    <div class="col-md-4">
        <BootstrapInputGroup>
            <BootstrapInputGroupLabel DisplayText="宽" />
            <BootstrapInputNumber @bind-Value="@Width" Min="20" Max="400" />
            <BootstrapInputGroupLabel DisplayText="cm(厘米)" />
        </BootstrapInputGroup>
    </div>
    <div class="col-md-4">
        <BootstrapInputGroup>
            <BootstrapInputGroupLabel DisplayText="厚" />
            <BootstrapInputNumber @bind-Value="@Height" IsDisabled="true" />
            <BootstrapInputGroupLabel DisplayText="cm(厘米)" />
        </BootstrapInputGroup>
    </div>
</div>


<Row ItemsPerRow="ItemsPerRow.Two">

    <BootstrapInputGroup>
        <BootstrapInputGroupLabel DisplayText="面料" />
        <MaterialComponent @bind-SelectedMaterial="@Fabric" TypeFilter="@MaterialType.面料"></MaterialComponent>
    </BootstrapInputGroup>

    <BootstrapInputGroup>
        <BootstrapInputGroupLabel DisplayText="包装" />
        <Select @bind-Value="@SelectPackage"></Select>
    </BootstrapInputGroup>

    <BootstrapInputGroup>
        <BootstrapInputGroupLabel DisplayText="舒适层海绵" />
        <MaterialComponent @bind-SelectedMaterial="@ShuShi" TypeFilter="@MaterialType.海绵"></MaterialComponent>
    </BootstrapInputGroup>

    <BootstrapInputGroup>
        <BootstrapInputGroupLabel DisplayText="舒适层厚度" />
        <BootstrapInputNumber @bind-Value="@ShuShiHeight" Min="0" Max="15"/>
        <BootstrapInputGroupLabel DisplayText="cm" />
    </BootstrapInputGroup>

    <BootstrapInputGroup>
        <BootstrapInputGroupLabel DisplayText="支撑层海绵" />
        <MaterialComponent @bind-SelectedMaterial="@ZhiCheng" TypeFilter="@MaterialType.海绵"></MaterialComponent>
    </BootstrapInputGroup>

    <BootstrapInputGroup>
        <BootstrapInputGroupLabel DisplayText="支撑层厚度" />
        <BootstrapInputNumber @bind-Value="@ZhiChengHeight" Min="0" Max="15"/>
        <BootstrapInputGroupLabel DisplayText="cm" />
    </BootstrapInputGroup>

</Row>

@code {
    float Length = 200;
    float Width = 180;
    float Height = 5;

    Package SelectPackage;

    Material? Fabric;
    Material? ShuShi;
    Material? ZhiCheng;

    float _ShuShiHeight = 2f;
    private float ShuShiHeight
    {
        get => _ShuShiHeight;
        set
        {
            if(value + ZhiChengHeight > 15f)
            {
                _ShuShiHeight = 15f - ZhiChengHeight;
            }
            else
            {
                _ShuShiHeight = value;
            }
            Height = ShuShiHeight + ZhiChengHeight;
        }
    }

    float _ZhiChengHeight = 3f;
    float ZhiChengHeight
    {
        get => _ZhiChengHeight;
        set
        {
            if(value + _ShuShiHeight > 15f)
            {
                _ZhiChengHeight = 15f - ShuShiHeight;
            }
            else
            {
                _ZhiChengHeight = value;
            }
            Height = ShuShiHeight + ZhiChengHeight;
        }
    }

    float Price;

    MattressConfig? config;

    float Price_JingXiao;

    bool Show_JingXiao;

    protected override async Task OnInitializedAsync()
    {
        string path = Path.Combine(even.WebRootPath, "configs", "global.json");
        //config = JsonSerializer.Deserialize<MattressConfig>(File.ReadAllText(path));
        var c = await cons.QueryAsync(new QueryPageOptions());
        if(c.Items is not null)
        {
            config = c.Items.FirstOrDefault();
        }
    }

    protected override bool ShouldRender()
    {
        float w = Width / 100f;
        float l = Length / 100f;

        Price = 0;

        if (Fabric is not null && (ShuShi is not null || ZhiCheng is not null) && config is not null)
        {
            //面料
            Price += Fabric.Price * MathF.Max(w, l) * 1.2f;

            //防滑底 10元每米
            Price += MathF.Max(w, l) * config.FangHua;

            //内套
            Price += (MathF.Max(w, l) + Height / 100f) * 2f * config.NeiTao;


            if (ShuShi is not null && ShuShiHeight > 0)
            {
                Price += ShuShi.Price * (w * l * (ShuShiHeight / 100f));
            }

            if (ZhiCheng is not null && ZhiChengHeight > 0)
            {
                Price += ZhiCheng.Price * (w * l * (ZhiChengHeight / 100f));
            }

            //损耗
            Price *= (1.0f+ (config.SunHao /100f));

            //包装
            switch (SelectPackage)
            {
                case Package.牛皮纸: Price += config.NiuPi; break;
                case Package.纸箱: Price += config.ZhiXiang; break;
            }

            //胶水，拉链,标
            Price += config.JiaoShui;
            Price += config.LaLian;
            Price += config.Biao;

            //边际贡献 45%
            Price /= (1.0f - (config.Bianji/100f));

            //税率
            Price *= (1.0f + (config.Shui/100f));

            //人工
            Price += config.RenGong;

            //经销商价格
            Price_JingXiao = (float)Math.Round(Price, 0);

            //零售系数
            Price *= config.LingShou;

            //零售价去小数
            Price = (float)Math.Round(Price, 0);
        }
        return base.ShouldRender();
    }

}
